package cn.com.ghy.android.widget.plan;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.ghy.android.bean.MyWayBean;
import cn.com.ghy.android.bean.VisitPlanWorkTypeBean;
import cn.com.ghy.android.widget.R;
import cn.com.ghy.android.bean.VisitPlanBean;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:下月拜访计划弹出修改菜单
 * @author wuz  @date 2011-5-25 11:16:53
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class VisitPlanPop extends Activity {

	private TextView labTxtMonth = null;
	private TextView labTxtWeek = null;
	private Spinner spiWay = null;
	private Spinner spiWorkType = null;
	private Button btnSubmit = null;
	private Button btnCancel = null;

	private Intent intent = null;
	private Bundle bundle = null;
	private List<MyWayBean> myWayList = null;
	private VisitPlanBean visitPlanBean = null;
	private List<VisitPlanWorkTypeBean> visitPlanWorkTypeList = null;
	private int position = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.visit_plan_pop);
		getSession();
		findView();
		init();
		addListener();
	}

	@SuppressWarnings("unchecked")
	private void getSession() {
		bundle = this.getIntent().getExtras();
		myWayList = (List<MyWayBean>) bundle.getSerializable("myWayList");
		visitPlanWorkTypeList = (List<VisitPlanWorkTypeBean>) bundle
				.getSerializable("visitPlanWorkTypeList");
		visitPlanBean = (VisitPlanBean) bundle.getSerializable("visitPlanBean");
		position = bundle.getInt("position");
	}

	private void findView() {
		labTxtMonth = (TextView) this.findViewById(R.id.labTxtMonth);
		labTxtWeek = (TextView) this.findViewById(R.id.labTxtWeek);
		spiWay = (Spinner) this.findViewById(R.id.spiWay);
		spiWorkType = (Spinner) this.findViewById(R.id.spiWorkType);
		btnSubmit = (Button) this.findViewById(R.id.btnSubmit);
		btnCancel = (Button) this.findViewById(R.id.btnCancel);
	}

	private void init() {
		labTxtMonth.setText(visitPlanBean.getVsdate() + "");
		labTxtWeek.setText(visitPlanBean.getWk());

		myWayList.add(new MyWayBean(-1 + "", "无"));
		ArrayAdapter<MyWayBean> wayAdapter = new ArrayAdapter<MyWayBean>(VisitPlanPop.this,
				android.R.layout.simple_spinner_item, myWayList);
		wayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiWay.setAdapter(wayAdapter);
		visitPlanWorkTypeList.add(new VisitPlanWorkTypeBean(-1, "无"));
		ArrayAdapter<VisitPlanWorkTypeBean> workTypeAdapter = new ArrayAdapter<VisitPlanWorkTypeBean>(
				VisitPlanPop.this, android.R.layout.simple_spinner_item, visitPlanWorkTypeList);
		workTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiWorkType.setAdapter(workTypeAdapter);

		if (null != visitPlanBean.getMyWayBean().getWayid()) {
			spiWay.setSelection(getSpinnerSelectedItemPosition(visitPlanBean.getMyWayBean()), true);
		}
		if (null != visitPlanBean.getVisitPlanWorkTypeBean().getWorkTypeName()) {
			spiWorkType.setSelection(
					getSpinnerSelectedItemPosition(visitPlanBean.getVisitPlanWorkTypeBean()), true);
		}

	}

	private void addListener() {

		spiWay.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				MyWayBean bean = (MyWayBean) parent.getItemAtPosition(position);
				if (!"-1".equals(bean.getWayid())) {
					spiWorkType.setSelection(0, true);
				}
				visitPlanBean.setMyWayBean(bean);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});

		spiWorkType.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				VisitPlanWorkTypeBean bean = (VisitPlanWorkTypeBean) parent
						.getItemAtPosition(position);
				if (bean.getWorkTypeId() != 10) {
					spiWay.setSelection(
							getSpinnerSelectedItemPosition(new MyWayBean(-1 + "", "无")), true);
				}
				visitPlanBean.setVisitPlanWorkTypeBean(bean);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}

		});

		btnSubmit.setOnClickListener(btnListener);
		btnCancel.setOnClickListener(btnListener);
	}

	private View.OnClickListener btnListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Button button = (Button) v;
			switch (button.getId()) {
				case R.id.btnSubmit :
					if ("-1".equals(visitPlanBean.getMyWayBean().getWayid())
							&& 10 == visitPlanBean.getVisitPlanWorkTypeBean().getWorkTypeId()) {
						Toast.makeText(VisitPlanPop.this, "拜访时必须选择所要拜访的路线!", Toast.LENGTH_LONG)
								.show();
						break;
					}
					intent = new Intent();
					bundle = new Bundle();
					bundle.putSerializable("visitPlanBean", visitPlanBean);
					bundle.putInt("position", position);
					intent.putExtras(bundle);
					VisitPlanPop.this.setResult(RESULT_OK, intent);
					VisitPlanPop.this.finish();
					break;
				case R.id.btnCancel :
					VisitPlanPop.this.finish();
					break;
			}

		}
	};

	private int getSpinnerSelectedItemPosition(MyWayBean bean) {
		int position = 0;
		for (int i = 0; i < myWayList.size(); i++) {
			if (bean.getWayid().equals(myWayList.get(i).getWayid())) {
				position = i;
			}
		}
		return position;
	}

	private int getSpinnerSelectedItemPosition(VisitPlanWorkTypeBean bean) {
		int position = 0;
		for (int i = 0; i < visitPlanWorkTypeList.size(); i++) {
			if (bean.getWorkTypeId() == visitPlanWorkTypeList.get(i).getWorkTypeId()) {
				position = i;
			}
		}
		return position;
	}
}
